package com.mall.b2c.controller;

import com.mall.b2c.domain.store.StoreCityDomain;
import com.mall.b2c.service.StoreCityService;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Message;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.controller.BaseController;
import com.mall.common.exception.NoticeException;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.Validations;
import com.mall.common.web.Servlets;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/b2c/storeCity")
public class StoreCityController  extends BaseController{

    @Resource
    private StoreCityService storeCityService;

    /**
     * 跳转到列表页面
     */
    @RequestMapping(value="/list", method= RequestMethod.GET)
    public String list(){
        return "/admin/b2c/storeCity/list";
    }

    /**
     * 获取列表信息
     */
    @RequestMapping(value="/queryData", method=RequestMethod.POST)
    @ResponseBody
    public Pager queryData(Pager pager, HttpServletRequest request){
        Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
        List<SearchFilter> filters = SearchFilter.parse(filterParams);
        pager = storeCityService.findPage(pager, filters);
        return JsonDataGridHelper.createJSONData(pager);
    }

    @RequestMapping(value="/add", method=RequestMethod.GET)
    public String add(){
        return "/admin/b2c/storeCity/input";
    }


    /**
     * 编辑
     */
    @Validations(
            requiredFields = {
                    @RequiredFieldValidator(fieldName = "id", message = "ID非法!")
            }
    )
    @RequestMapping(value="/edit", method=RequestMethod.GET)
    public String edit(Long id, Model model){
        StoreCityDomain city = storeCityService.find(id);
        model.addAttribute("city", city);
        return "/admin/b2c/storeCity/input";
    }

    @Validations(
            requiredFields = {
                    @RequiredFieldValidator(fieldName = "name", message = "请选择城市名称")
            }
    )
    @RequestMapping(value="/checkCityName", method = RequestMethod.GET)
    @ResponseBody
    public Boolean checkName(String oldName) {
        return storeCityService.isExist("name", oldName);
    }
    /**
     * 新增
     */
    @Validations(
            requiredFields = {
                    @RequiredFieldValidator(fieldName = "name", message = "请选择城市名称")
            }
    )
    @RequestMapping(value="/save", method=RequestMethod.POST)
    @ResponseBody
    public String save(StoreCityDomain city, HttpServletResponse response){
        if(storeCityService.isExist("name", city.getName())){
            return ResponseUtils.renderJSON(Message.error("城市名称已存在", new Object[0]), response);
        }
        storeCityService.persist(city);
        return ResponseUtils.renderJSON(successMessage, response);
    }


    /**
     * 修改
     */
    @Validations(
            requiredFields = {
                    @RequiredFieldValidator(fieldName = "id", message = "ID非法!")
            }
    )
    @RequestMapping(value="/update", method=RequestMethod.POST)
    @ResponseBody
    public String update(StoreCityDomain city, HttpServletResponse response){
        StoreCityDomain dbCity = storeCityService.find(city.getId());
        if(dbCity == null){
            throw new NoticeException("该信息不存在");
        }

        dbCity.setXpoint(city.getXpoint());
        dbCity.setYpoint(city.getYpoint());
        dbCity.setName(city.getName());
        dbCity.setUname(city.getUname());
        dbCity.setPriority(city.getPriority());
//        dbCity.setIs_default(city.getIs_default());
        dbCity.setIs_effect(city.getIs_effect());

        storeCityService.update(dbCity);
        return ResponseUtils.renderJSON(successMessage, response);
    }

    /**
     * 删除
     */
    @Validations(
            requiredFields = {
                    @RequiredFieldValidator(fieldName = "ids", message = "ids非法")
            }
    )
    @RequestMapping(value="/delete", method=RequestMethod.POST)
    @ResponseBody
    public String delete(Long[] ids, HttpServletResponse response){
        storeCityService.remove(ids);
        return ResponseUtils.renderJSON(successMessage, response);
    }
}
